package org.example.lang;

import org.example.domain.R;
import org.example.exception.BusinessException;
import org.example.utils.Func;

/**
 * @author: sql
 * @date: 2024/6/7
 * @Description: 断言，断言某些对象或值是否符合规定，否则抛出异常。经常用于做变量检查
 * @version: 1.0
 */
public class AssertUtil {

    /**
     * 大于O
     */
    public static void gtz(Integer num, String error) {
        if (num == null || num > 0) {
            throw new BusinessException(error);
        }
    }

    /**
     * 大于等于O
     */
    public static void gez(Integer num, String error) {
        if (num == null || num >= 0) {
            throw new BusinessException(error);
        }
    }

    /**
     * num1大于num2
     */
    public static void gt(Integer num1, Integer num2, String error) {
        if (num1 > num2) {
            throw new BusinessException(error);
        }
    }

    /**
     * num1大于等于num2
     */
    public static void ge(Integer num1, Integer num2, String error) {
        if (num1 >= num2) {
            throw new BusinessException(error);
        }
    }

    /**
     * 小于O
     */
    public static void ltz(Integer num, String error) {
        if (num == null || num < 0) {
            throw new BusinessException(error);
        }
    }

    /**
     * 小于等于O
     */
    public static void lez(Integer num, String error) {
        if (num == null || num <= 0) {
            throw new BusinessException(error);
        }
    }

    /**
     * num1小于num2
     */
    public static void lt(Integer num1, Integer num2, String error) {
        if (num1 < num2) {
            throw new BusinessException(error);
        }
    }

    /**
     * num1小于等于num2
     */
    public static void le(Integer num1, Integer num2, String error) {
        if (num1 <= num2) {
            throw new BusinessException(error);
        }
    }

    /**
     * 判断 expression 是否为 True,如果为真则抛出错误 IError
     *
     * @param expression 判断的条件
     * @param error      要抛出的错误
     */
    public static void isTrue(boolean expression, String error) {
        if (expression) {
            throw new BusinessException(error);
        }
    }

    public static void isTrue(boolean expression, Msg error) {
        if (expression) {
            throw new BusinessException(error.getCode(), error.getMsg(), error.getExtend());
        }
    }

    /**
     * 判断 expression 是否为 False,如果为假则抛出错误 False
     *
     * @param expression 判断的条件
     * @param error      要抛出的错误
     */
    public static void isFalse(boolean expression, String error) {
        if (!expression) {
            throw new BusinessException(error);
        }
    }

    public static void isFalse(boolean expression, Msg error) {
        if (!expression) {
            throw new BusinessException(error.getCode(), error.getMsg(), error.getExtend());
        }
    }

    /**
     * 判断目标对象是空的，则抛出 IError
     *
     * @param obj   目标对象
     * @param error 要抛出的错误
     * @param <T>   返回的数据类型
     * @return T
     */
    public static <T> T isEmpty(T obj, String error) {
        isTrue(Func.isEmpty(obj), error);
        return obj;
    }

    public static <T> T isEmpty(T obj, Msg error) {
        isTrue(Func.isEmpty(obj), error);
        return obj;
    }

    public static <T> T isNotEmpty(T obj, String error) {
        isTrue(Func.isNotEmpty(obj), error);
        return obj;
    }


    /**
     * 判断目标对象不为空是，则抛出IError
     *
     * @param obj   目标对象
     * @param error 要抛出的错误
     */
    public static void notEmpty(Object obj, String error) {
        isTrue(Func.isNotEmpty(obj), error);
    }

    /**
     * 判断结果是否错误，错误抛出结果中的异常信息
     *
     * @param result 判断的结果信息
     */
    public static void isError(R<?> result) {
        if (result.isError()) {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
    }
}
